﻿using System;

using HsTec.Mobile.Green.Util;
using System.Collections.Generic;
using HsTec.Mobile.Green.DAL;

public partial class ReportTemplate_ImportTemplate : System.Web.UI.Page
{
    ReportTemplateDAL dal = new ReportTemplateDAL();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            DDLTypeBind();
        }
    }

    private void DDLTypeBind()
    {
        DDLType.DataSource = dal.GetList("FieldParent=0");
        DDLType.DataTextField = "FieldAlias";
        DDLType.DataValueField = "RT_ID";
        DDLType.DataBind();
        DDLType.Items.Insert(0, "请选择...");
        DDLType.Items[0].Value = "";
    }
    protected void btn_Statistics_Click(object sender, EventArgs e)
    {
        //1.提交前的判断
        if (hidType.Value == "1")
        {
            if (String.IsNullOrEmpty(txtTableName.Text))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('表名不能为空！');</script>");
                return;
            }
            if (String.IsNullOrEmpty(txtAlias.Text))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('别名不能为空！');</script>");
                return;
            }
            if (String.IsNullOrEmpty(sltTableType.Value))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('请选择表的类型！');</script>");
                return;
            }
        }
        //2.验证上传文件类型
        string UpfileExt = System.IO.Path.GetExtension(FU_Statistics.FileName);
        if (UpfileExt != ".xls")
        {
            Text_Confirm_Upstate.Text = "模板的文件名类型不符要求！（必须是EXCEL文件）";
            return;
        }
        //3.文件上传
        switch (Util.Upload_File(FU_Statistics, FU_Statistics.FileName, "Phase2\\Template\\"))
        {
            case 1:
                Text_Confirm_Upstate.Text = "上传文件成功！";
                break;
            case 0:
                Text_Confirm_Upstate.Text = "上传文件失败！";
                return;
        }
        //4.分析上传文件，并导入数据库
        Text_Confirm_Upstate.Text = String.Empty;
        ReportHelper rhelper = new ReportHelper();
        //指明数据表对应的EXCEL文件
        rhelper.excelPath = "Phase2\\Template\\" + FU_Statistics.FileName;
        Dictionary<string, string> dicRtn = new Dictionary<string, string>();
        Text_Confirm_Upstate.Text = rhelper.importTemplate(txtTableName.Text.Trim(), txtAlias.Text.Trim(), sltTableType.Value, hidType.Value.Trim(),DDLType.SelectedValue.Trim(), ref dicRtn);
    }
}
